import dayjs from 'dayjs'
Component({
    properties: {
        /**
         * 1:年
         * 2:月
         * 3:日
         */
        type: {
            type: Number,
            value: 3
        },
        showtit: {
            type: Boolean,
            value: true
        }
    },
    data: {
        days: [],
        months: [],
        years: [],
        daylength: 31,
        value: [0, 0, 0],
    },
    lifetimes: {
        // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
        attached: function () {
            this._initTimeArray()
            console.log(1111)
            this.setDefaultValue()
        },
        moved: function () {},
        detached: function () {},
    },
    observers: {
        'type': function () {
            setTimeout(() => {
                this.setDefaultValue()
            }, 0)
        }
    },
    methods: {
        setDefaultValue() {
            let y = dayjs().year()
            let m = dayjs().month() + 1
            let d = dayjs().date()
            this.data.value[0] = this.findIndex(y, this.data.years)
            this.data.value[1] = this.findIndex(m, this.data.months)
            this.data.value[2] = this.findIndex(d, this.data.days)
            this.setData({
                value: this.data.value
            })
            this.bindChange({
                detail: {
                    value: this.data.value
                }
            })
        },
        findIndex(value, arr) {
            let i = arr.findIndex((ele) => {
                return ele === value
            })
            return i === -1 ? arr.length - 1 : i
        },
        /**
         * 对外开发的方法（事件）
         */
        bindChange(e) {
            const val = e.detail.value
            let len = dayjs(this.data.years[val[0]] + '-' + this.data.months[val[1]]).daysInMonth()
            this.setData({
                daylength: len
            })
            this._initTimeArray()
            // 时间选择器滑动事件，参数：当前选择的时间
            if (this.data.type === 1) {
                this.triggerEvent('Change', {
                    date: this.data.years[val[0]]
                })
            } else if (this.data.type === 2) {
                this.triggerEvent('Change', {
                    date: this.data.years[val[0]] + '-' + ((this.data.months[val[1]] + '').length === 1 ? '0' + this.data.months[val[1]] : this.data.months[val[1]])
                })
            } else if (this.data.type === 3) {
                this.triggerEvent('Change', {
                    date: this.data.years[val[0]] + '-' + ((this.data.months[val[1]] + '').length === 1 ? '0' + this.data.months[val[1]] : this.data.months[val[1]]) + '-' + ((this.data.days[val[2]] < 10 ? '0' + this.data.days[val[2]] : this.data.days[val[2]]))
                })
            }
            // } else if (this.data.type === 2) {
            //     this.triggerEvent('Change', {
            //         date: this.data.years[val[0]] + '-' + this.data.months[val[1]]
            //     })
            // } else if (this.data.type === 3) {
            //     this.triggerEvent('Change', {
            //         date: this.data.years[val[0]] + '-' + this.data.months[val[1]] + '-' + this.data.days[val[2]]
            //     })
            // }
        },

        /**
         * 内部方法
         */
        _initTimeArray() {
            const date = new Date()
            const years = []
            const months = []
            const days = []
            // const hours = [];
            // const minutes = [];
            // const seconds = [];

            for (let i = 1990; i <= date.getFullYear(); i++) {
                years.push(i)
            }
            for (let i = 1; i <= 12; i++) {
                months.push(i)
            }
            for (let i = 1; i <= this.data.daylength; i++) {
                days.push(i)
            }
            // for (let i = 1; i <= 23; i++) {
            //     hours.push(i)
            // }
            // for (let i = 1; i <= 59; i++) {
            //     minutes.push(i)
            // }
            // for (let i = 1; i <= 59; i++) {
            //     seconds.push(i)
            // }

            this.setData({
                years,
                months,
                days,
                // hours,
                // minutes,
                // seconds
            })
        },
    },
})